package com.infomanage.service.worklog;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.DutypersonDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.Dutyperson;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.util.TimeUtil;
import com.infomanage.vo.DutypersonItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

public class DutyPersonService {

	public static DutypersonItem loadDutyperson(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId= request.getParameter("itemId");
			if(StringUtils.isEmpty(itemId)) return null;
			DutypersonDAO dao = new DutypersonDAO();
			return dao.find(Integer.parseInt(itemId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static DutypersonItem loadTodayDutyperson(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			DutypersonDAO dao = new DutypersonDAO();
			SearchBean searchBean = new SearchBean();
			
			FieldBean beginTimeField = new FieldBean();
			beginTimeField.setLike(true);
			beginTimeField.setName("beginTime");
			beginTimeField.setValue(TimeUtil.showCurrentDay());
//			FieldBean endTimeField = new FieldBean();
//			endTimeField.setLike(true);
//			endTimeField.setName("endTime");
//			endTimeField.setValue(TimeUtil.showCurrentDay());
			searchBean.add(beginTimeField);
			List<DutypersonItem> list = dao.findByFields(searchBean);
			if(list != null && list.size() > 0){
				return list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<DutypersonItem> loadDutypersonList(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			DutypersonDAO dao = new DutypersonDAO();
			List<DutypersonItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject saveDutypersonInfo(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			Dutyperson bean = new Dutyperson();
			bean.setMasterPersonId(Integer.parseInt(request.getParameter("masterPersonId")));
			bean.setSlavePersonId(Integer.parseInt(request.getParameter("slavePersonId")));
			bean.setAttendantPersonId(Integer.parseInt(request.getParameter("attendantPersonId")));
			bean.setDispatcherPersonId(Integer.parseInt(request.getParameter("dispatcherPersonId")));
			bean.setBeginTime(request.getParameter("beginTime"));
			bean.setEndTime(request.getParameter("endTime"));
			DutypersonDAO dao = new DutypersonDAO();
			dao.add(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
			e.printStackTrace();
		}
		return ret;
	}
	
	public static ReturnObject modifyDutypersonInfo(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int itemId = Integer.parseInt(request.getParameter("itemId"));
			DutypersonDAO dao = new DutypersonDAO();
			Dutyperson bean = dao.find(Dutyperson.class, itemId);
			bean.setMasterPersonId(Integer.parseInt(request.getParameter("masterPersonId")));
			bean.setSlavePersonId(Integer.parseInt(request.getParameter("slavePersonId")));
			bean.setAttendantPersonId(Integer.parseInt(request.getParameter("attendantPersonId")));
			bean.setDispatcherPersonId(Integer.parseInt(request.getParameter("dispatcherPersonId")));
			bean.setBeginTime(request.getParameter("beginTime"));
			bean.setEndTime(request.getParameter("endTime"));
			dao.modify(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject removeDutypersonInfo(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId = request.getParameter("itemId");
			DutypersonDAO dao = new DutypersonDAO();
			dao.remove(Dutyperson.class, Integer.parseInt(itemId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
			e.printStackTrace();
		}
		return ret;
	}
	
	public static ReturnObject removeBatchDutypersonInfo(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			DutypersonDAO dao = new DutypersonDAO();
			if(batchIds != null){
				String[] ids = batchIds.split(";");
				for(String id : ids){
					dao.remove(Dutyperson.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static List<DutypersonItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			DutypersonDAO dao = new DutypersonDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			//设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if(currPageNum == null){
				page.setCurrentPageNum(1);
			}else{
				page.setCurrentPageNum(Integer.parseInt(currPageNum));
				
			}
			searchBean.setPage(page);
			List<DutypersonItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			DutypersonDAO dao = new DutypersonDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)/PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String beginDay = request.getParameter("beginDay");
		String endDay = request.getParameter("endDay");
		SearchBean searchBean = new SearchBean();
		if (!StringUtil.isEmpty(beginDay)) {
			searchBean.setBeginTimeName("beginTime");
			searchBean.setBeginTimeValue(beginDay);
		}
		if (!StringUtil.isEmpty(endDay)) {
			searchBean.setEndTimeName("endTime");
			searchBean.setEndTimeValue(endDay);
		}
		//设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if(user != null){
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
	
	public static String getLastEndTime(HttpServletRequest request, HttpServletResponse response,
			JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			SearchBean searchBean = new SearchBean();
			//设置权限
			User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
			if(user != null){
				AuthorityBean authority = new AuthorityBean();
				authority.setRole(RoleType.valueOf(user.getRoleId()));
				authority.setDeptId(user.getAssociatedDept());
				searchBean.setAuthority(authority);
			}
			DutypersonDAO dao = new DutypersonDAO();
			List<DutypersonItem> items = dao.findByFields(searchBean);
			if(items != null && items.size() > 0){
				return items.get(items.size() - 1).getBean().getEndTime();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}
